package org.ucas.kgraph.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ucas.kgraph.dto.*;
import org.ucas.kgraph.entity.File;
import org.ucas.kgraph.entity.Schem;
import org.ucas.kgraph.service.FileService;
import org.ucas.kgraph.service.SchemService;
import org.ucas.kgraph.service.TermService;
import org.ucas.kgraph.utils.exception.BadRequestException;
import org.ucas.kgraph.utils.exception.NotFoundException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Null;

import java.io.IOException;
import java.util.List;

@RestController
public class FileController extends BaseController {

  @Autowired
  private FileService fileService;
  @Autowired
  private TermService termService;
  @Autowired
  private SchemService schemService;
  
  @RequestMapping(value = "/files/tree",method = RequestMethod.GET)
  public CarryDto<FileDto> fileList(@RequestParam(value = "parent_id",required = false) Long parent_id,@RequestParam(value = "account_id",required = false) Long account_id, @RequestParam("type") String type,@RequestParam(value = "state",required = false) String state){
    if(account_id==null){
    	return CarryDto.of(FileDto.of(fileService.getFileListAll(type,state)));
    }else{
    	return CarryDto.of(FileDto.of(fileService.getFileListAll(account_id,type,state)));
    }
	
  }
  
  @RequestMapping(value = "/files/list",method = RequestMethod.GET)
  public ListDto<FileDto> files(@RequestParam(value = "file_id") Long file_id,@RequestParam(value = "account_id",required = false) Long account_id, @RequestParam("type") String type,@RequestParam(value = "state",required = false) String state){
	  file(file_id);
	  if(account_id==null){
		  return ListDto.of(fileService.getFileListByParent(file_id,type,state),FileDto::ofroot);
	  }else{
		  return ListDto.of(fileService.getFileListByUser(file_id,account_id,type,state),FileDto::ofroot);
	  }
  }
  
  @RequestMapping(value = "/files/listbySearch",method = RequestMethod.GET)
  public ListDto<FileDto> filesBySearch(@RequestParam("type") String type,@RequestParam(value = "state",required = false) String state,@RequestParam("searchString") String searchString){

	  return ListDto.of(fileService.getFileListBySearch(file(),type,state,searchString),FileDto::ofroot);
  }
  
  
  @RequestMapping(value = "/files",method = RequestMethod.POST)
  public CarryDto<FileDto> create(@RequestParam(value = "name") String file_name,@RequestParam(value = "account_id") Long account_id, @RequestParam("type") String type,@RequestParam(value = "state") String state){
	File tmp = new File();
	File fileParent=fileService.load(file());
	if(fileParent==null){
		tmp.setParent_id(Long.valueOf(0));
		tmp.setLevel(1);
	}else{
		tmp.setParent_id(fileParent.getId());
		tmp.setLevel(fileParent.getLevel()+1);
	}
	if(tmp.getLevel()>5){
		throw BadRequestException.File_LevelMuch;
	}
	tmp.setAccount_id(account_id);
	tmp.setState(state);
	tmp.setName(file_name);
	tmp.setType(type);
	fileService.create(tmp);
	FileDto dto = FileDto.ofroot(tmp);
    return CarryDto.of(dto);
  }
  @RequestMapping(value = "/files/{id:\\d+}/delete",method = RequestMethod.DELETE)
  public ResponseDto delete(@PathVariable Long id){
	  
	  //if(fileService.getFileListByAllParent(id)==null&&termService.listRoots(id)==null&&schemService.listRootAllByFile(id)==null&&!id.equals(Long.valueOf(0))){
	  System.out.println("1:"+fileService.getFileListByAllParent(id).isEmpty());
	  System.out.println("2:"+termService.listRoots(id).isEmpty());
	  System.out.println("1:"+fileService.getFileListByAllParent(id).isEmpty());
	  System.out.println("1:"+fileService.getFileListByAllParent(id).isEmpty());
	  System.out.println("1:"+fileService.getFileListByAllParent(id).isEmpty());
	  if(fileService.getFileListByAllParent(id).isEmpty()&&termService.listRoots(id).isEmpty()&&schemService.listRootAllByFile(id).isEmpty()&&!id.equals(Long.valueOf(0))){
		  fileService.delete(id);
		  return ResponseDto.OK;
	  }else{
		  throw NotFoundException.Term_NotExists;
	  }
    
  }
  @RequestMapping(value = "/files/{id:\\d+}/rename",method = RequestMethod.PUT)
  public  ResponseDto reroot(@PathVariable Long id,@RequestParam("newname") String newname) throws JsonParseException, JsonMappingException, IOException {
	  File file = fileService.rename(id,newname);
		return  ResponseDto.OK;
  }
  
  
}
